<template>
	<view class="container">
		<view class="yy-mz-head">
			<view class="yy-mz-headItem" @click="changedata(index,item.yearDate)" v-for="(item,index) in dataList"
				:key="index" :class="[currnum == index?'yy-mz-headShow':'']">
				<text>{{item.date}}</text>
				<text>{{item.week}}</text>
			</view>
		</view>
		<div class="doc-pb-conprve" style="cursor: pointer;" @click="getprvelist">导入上一次排班</div>
		<view class="doc-pb-con">
			<!-- 上午 -->
			<view class="doc-pb-conday">
				<view class="doc-pb-condayTitle">
					<text class="doc-pb-condayTitleAm">上午</text>
					<text class="doc-pb-condayTitleAmtime">{{amtime.start}}-{{amtime.end}}</text>
				</view>
				<view class="doc-pb-condayList">
					<uni-swipe-action>
						<uni-swipe-action-item v-for="(item,index) in AMlist" v:key="index" class="uniswiper"
							:right-options="options" @click="onClick(item.id)">
							<view class="doc-pb-condayItem">
								<view class="doc-pb-condayItem-l">
									<text class="">{{item.time_text}}</text>
								</view>
								<view class="doc-pb-condayItem-r">{{item.number}}人</view>
							</view>
						</uni-swipe-action-item>
					</uni-swipe-action>
					<picker style="margin-top: 8rpx;" mode="time" @change="choicetime" :start="amtime.start"
						:end="amtime.end">
						<view class="doc-pb-condayAdd doc-pb-condayMa" @click="choosetime">
							<view class="doc-pb-condayAddIcon">
								<image src="../../static/icon_plus2.png" mode="widthFix"></image>
							</view>
							<text class="doc-pb-condayAddTitle">新增时间段</text>
						</view>
					</picker>
					<!-- <view class="doc-pb-condayAdd doc-pb-condayMa" @click="addtime">
						<view class="doc-pb-condayAddIcon">
							<image src="../../static/icon_plus2.png" mode="widthFix"></image>
						</view>
						<text class="doc-pb-condayAddTitle">新增时间段</text>
					</view> -->
				</view>
			</view>
			<!-- 下午 -->
			<view class="doc-pb-conday doc-pb-conMa">
				<view class="doc-pb-condayTitle">
					<text class="doc-pb-condayTitleAm">下午</text>
					<text class="doc-pb-condayTitleAmtime">{{pmtime.start}}-{{pmtime.end}}</text>
				</view>
				<view class="doc-pb-condayList">
					<uni-swipe-action>
						<uni-swipe-action-item class="uniswiper" :right-options="options" v-for="(item,index) in PMlist"
							:key='index' @click="onClick(item.id)">
							<view class="doc-pb-condayItem">
								<view class="doc-pb-condayItem-l">
									<text class="">{{item.time_text}}</text>
								</view>
								<view class="doc-pb-condayItem-r">{{item.number}}人</view>
							</view>
						</uni-swipe-action-item>
					</uni-swipe-action>
					<!-- <view class="doc-pb-condayAdd doc-pb-condayMa" @click="choosetime">
						<view class="doc-pb-condayAddIcon">
							<image src="../../static/icon_plus2.png" mode="widthFix"></image>
						</view>
						<text class="doc-pb-condayAddTitle">新增时间段</text>
					</view> -->
					<picker style="margin-top: 8rpx;" mode="time" @change="choicetime" :start="pmtime.start"
						:end="pmtime.end">
						<view class="doc-pb-condayAdd doc-pb-condayMa" @click="choosetime">
							<view class="doc-pb-condayAddIcon">
								<image src="../../static/icon_plus2.png" mode="widthFix"></image>
							</view>
							<text class="doc-pb-condayAddTitle">新增时间段</text>
						</view>
					</picker>
				</view>
			</view>
		</view>
		<w-picker :visible.sync="pernum" default-type="name" :default-props="defaultProps" mode="selector"
			:options="pernumList" @confirm="changehos($event)"></w-picker>
	</view>
</template>

<script>
	import app from '../../App.vue'
	export default {
		data() {
			return {
				AMlist: [],
				PMlist: [],
				options: [{
					text: '删除',
					style: {
						backgroundColor: '#dd524d'
					}
				}],
				amtime: {},
				pmtime: {},
				dataList: [],
				currnum: 0,
				week: '',
				data:'',
				num: '',
				pernum: false,
				defaultProps: {
					"label": "name",
					"value": "id"
				},
				pernumList: [{
						name: 1
					},
					{
						name: 2
					},
					{
						name: 3
					},
					{
						name: 4
					},
					{
						name: 5
					},
					{
						name: 6
					},
					{
						name: 7
					},
					{
						name: 8
					},
					{
						name: 9
					},
					{
						name: 10
					},
				],
				datatime: '', //选择时间
				petientnum: '', //选择病人数量
			}
		},
		onLoad() {
			let start = new Date()
			let end = new Date()
			this.getnetDate(start, end, 40)
			this.getSchedultime()
		},
		onShow() {
			let data = this.dataList[0].yearDate
			this.gettime(data)
		},
		methods: {
			onClick(id) {
				let that = this,
					data = {
						user_scheduling_id: id
					}
				app.post('api/user_scheduling/delUserScheduling', data).then(res => {
					if (res.code == 1) {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							mask: true,
						})
						let time = this.currnum === 0?that.dataList[0].yearDate:this.data
						this.gettime(time)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							mask: true
						})
					}
				}).catch(err => {
					uni.showToast({
						title: err.msg,
						icon: 'none',
						mask: true
					})
				})
			},
			choosetime() {
				// this.pernum = true
			},
			getSchedultime() {
				let that = this,
					data = {
						type: '7'
					}
				app.post('api/common/getConfig', data).then(res => {
					if (res.code == 1) {
						console.log(res.data)
						that.amtime = res.data.time.am
						that.pmtime = res.data.time.pm
					}
				})
			},
			
			choicetime(e) {
				this.datatime = e.detail.value
				this.pernum = true
			},
			
			changehos(e) { //提交排版
				console.log(this.dataList[0].yearDate)
				let that = this,
					data = {
						date: that.currnum == 0 ? that.dataList[0].yearDate : that.data,
						time_text: that.datatime,
						number: e.obj.name
					}
				app.post('api/user_scheduling/addUserScheduling', data).then(res => {
					if (res.code == 1) {
						let date = that.currnum == 0 ? that.dataList[0].yearDate : that.data
						this.gettime(date)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							mask: true
						})
					}
				}).catch(err => {
					uni.showToast({
						title: err.msg,
						icon: 'none',
						mask: true
					})
				})
			},
			getnetDate(startDate, endDate, day) { //获取时间和周天
				var startDate = new Date(startDate.getTime()+2 * 24 * 3600 * 1000);
				var endDate =  new Date(startDate.getTime()+ day * 24 * 3600 * 1000);
				// endDate.setDate(startDate.getDate() + day)
				// console.log(startDate)
				// console.log(endDate)
				// console.log(startDate.getDate())
				// console.log(startDate.getDate() + day)
				// console.log(endDate.setDate(startDate.getDate() + day))
				var dataArr = [];
				var weeks = ['日', '一', '二', '三', '四', '五', '六'];
				var ids = ['0', '1', '2', '3', '4', '5', '6']
				// return
				while ((endDate.getTime() - startDate.getTime()) >= 0) {
					
					var year = startDate.getFullYear()
					
					
					var month = startDate.getMonth() + 1
					month = month.toString().length == 1 ? "0" + month.toString():month
					
					
					var day = startDate.getDate()
					day = day.toString().length == 1 ? '0' + day.toString():day
					
					var daay = startDate.getDate();
					
					var week = weeks[startDate.getDay() > 6 ? 0 : startDate.getDay()];
					var id = ids[startDate.getDay() > 6 ? 0 : startDate.getDay()];
					let ayr = {}
					ayr.date = month + '-' + day
					ayr.week = '周 ' + week
					ayr.yearDate = year + '-' + month + '-' + day
					ayr.id = id
					// ayr.day = daay
					dataArr.push(ayr)
					startDate.setDate(startDate.getDate() + 1);
				}
				console.log(dataArr, "日期");
				this.dataList = dataArr
				return dataArr
			},
			
			
			
			
			changedata(i,time) { //排版接诊列表
				this.currnum = i
				this.data = time
				let data = time
				this.gettime(data)
			},
			
			gettime(time) { //上午下午列表
				let that = this,
					data = {
						date: time
					}
				app.post('api/user_scheduling/getUserSchedulingList', data).then(res => {
					if (res.code == 1) {
						that.AMlist = res.data.am
						that.PMlist = res.data.pm
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none',
							mask: true
						})
					}
				}).catch(err => {
					uni.showToast({
						title: err.msg,
						icon: 'none',
						mask: true
					})
				})
			},
			
			getprvelist() {
				let that = this,
					data = {
						date: that.currnum == 0 ? that.dataList[0].yearDate : that.data
					}
				uni.showLoading({
					title: '导入中',
					success() {
						app.post('api/user_scheduling/addLast', data).then(res => {
							if (res.code == 1) {
								let date = that.currnum == 0 ? that.dataList[0].yearDate : that.data
								that.gettime(date)
								uni.hideLoading()
								console.log(111, '成功')
								// uni.showToast({
								// 	title: res.msg,
								// 	icon: 'none',
								// 	mask: true
								// })
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									mask: true
								})
								console.log(111, 'code不等与1')
							}
						}).catch(err => {
							console.log(err)
							uni.showToast({
								title: err.msg,
								icon: 'none',
								mask: true
							})
						})
					}
				})
			}
		}
	}
</script>

<style>
	@import url("../../common/docpaiban.css");
</style>
